package com.oracle.admin.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.oracle.admin.service.api.AdminServiceApi;
import com.oracle.admin.service.api.Api;
import com.oracle.mapper.AdminsMapper;
import com.oracle.mapper.RolesMapper;
import com.oracle.pojo.Admins;
import com.oracle.pojo.AdminsExample;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class AdminService implements AdminServiceApi {

    @Autowired
    private AdminsMapper adminsMapper;
    @Autowired
    private RolesMapper rolesMapper;

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<Admins> findAdminByLoginname(String loginname) {
        System.out.println(loginname);
        AdminsExample example = new AdminsExample();
        AdminsExample.Criteria criteria = example.createCriteria();
        criteria.andLoginnameEqualTo(loginname);
        List<Admins> adminsList = this.adminsMapper.selectByExample(example);
        System.out.println("执行结束");
        return new ResultEntity<Admins>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS, adminsList.isEmpty() ? null : adminsList.get(0));
    }

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<List<String>> getpermissionByLoginid(Integer id) {
        return new ResultEntity<List<String>>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS, this.rolesMapper.selectPermissionsByAdminid(id));
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ResultEntity updateAdmin(Admins admin) {
        this.adminsMapper.updateByPrimaryKeySelective(admin);
        return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
    }

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<List<Admins>> findAdminsAll() {
        List<Admins> adminsList = this.adminsMapper.selectByExample(null);
        return new ResultEntity<List<Admins>>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS, adminsList);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ResultEntity addAdminAndRoles(Admins admins, Integer[] roleids) {
        if (admins.getRolename() == null || "".equals(admins.getRolename())) {
            return new ResultEntity(Api.ADMIN_CODE_ROLENAME, "角色名参数不能为空");
        }
        if (admins.getLoginname() == null || "".equals(admins.getLoginname())) {
            return new ResultEntity(Api.ADMIN_CODE_LOGINNAME, "登录名参数不能为空");
        }
        if (admins.getPwd() == null || "".equals(admins.getPwd())) {
            return new ResultEntity(Api.ADMIN_CODE_PWD, "密码参数不能为空");
        }
        AdminsExample example = new AdminsExample();
        AdminsExample.Criteria criteria = example.createCriteria();
        criteria.andLoginnameEqualTo(admins.getLoginname());
        List<Admins> adminsPoList = this.adminsMapper.selectByExample(example);
        if (!adminsPoList.isEmpty()) {
            return new ResultEntity(Api.ADMIN_CODE_EXIST, "已存在此管理员的登录名");
        }
        /*
           添加管理员，返回主键
           进行管理员和角色的维护
         */
        admins.setCreatedate(new Timestamp(new Date().getTime()));
        admins.setStatus(0);
        admins.setFailcount(0);
        this.adminsMapper.insert(admins);
        if (roleids!=null) {
            for (int i = 0; i < roleids.length; i++) {
                this.adminsMapper.insertAdminAndRoles(admins.getId(), roleids[i]);
            }
        }
        return new ResultEntity(Api.ADMIN_CODE_SUCCESS, "成功");
    }

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<Admins> findAdminById(Integer id) {
        Admins admins = this.adminsMapper.selectByPrimaryKey(id);
        return new ResultEntity<Admins>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS, admins);
    }

    @Override
    public ResultEntity<Admins> deleteAdminById(Integer id) {
        this.adminsMapper.deleteByPrimaryKey(id);
        //维护管理员和角色关系表数据
        this.adminsMapper.deleteAdminRoleByAdminId(id);
        return new ResultEntity<Admins>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
    }

    @Override
    public ResultEntity<List<Integer>> getRolesByAdminId(Integer id) {
        List<Integer> roleids = this.adminsMapper.selectRolesByAdminId(id);
        return new ResultEntity<List<Integer>>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS, roleids);
    }

    @Override
    public ResultEntity updateAdminAndRoles(Admins admin, Integer... roleids) {
        //修改管理员信息
        this.adminsMapper.updateByPrimaryKeySelective(admin);
        //删除管理员的角色信息
        this.adminsMapper.deleteAdminRoleByAdminId(admin.getId());
        //更新管理员的角色信息
        if (roleids!=null) {
            for (int i = 0; i < roleids.length; i++) {
                this.adminsMapper.insertAdminAndRoles(admin.getId(), roleids[i]);
            }
        }
        return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
    }
}
